package au.com.handsunited.web.controller.group;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.model.Group;
import au.com.handsunited.model.Group;
import au.com.handsunited.service.GroupService;
import au.com.handsunited.web.session.SessionSupport;

public class GroupRegistrationController extends SimpleFormController {

	private GroupService groupService;

	private static Logger logger = LoggerFactory.getLogger(GroupRegistrationController.class);


	/**
	 *
	 */
	public GroupRegistrationController() {
		setCommandClass(Group.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		Group group = (Group) command;
		groupService.createGroup(group, SessionSupport.getUser(request));
		ModelAndView mav = new ModelAndView(getSuccessView());
		mav.addObject("entity", group);
		return mav;
	}


	@Override
	protected void initBinder (HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd-MM-yyyy"), true));
	}


	/**
	 *
	 * @param organisationService
	 */
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}
}
